package com.ag.contactTree;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.ag.bean.TbDeptType;
import com.ag.zhh.dao.DeptDAO;
import com.ag.zhh.dao.UserDeptDAO;
import com.ag.zhh.vo.Dept;
import com.ag.zhh.vo.UserDept;

/**
 * 应急部门通讯录树,保存在服务器内存 单例模式——线程安全
 * 
 * @author 张颢铧
 * 
 */

public class EmergencyTree extends AbstractTree implements Serializable {
	private static Logger logger = Logger.getLogger(EmergencyTree.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -3149071186653147609L;
	private volatile static EmergencyTree instance = null;
	private static Object lock = new Object();
	private EmergencyTree() {

	}

	public static EmergencyTree getInstance(UserDeptDAO deptDao) {
		if (instance == null) {
			
			synchronized (lock) {
				if (instance == null) {
					instance = new EmergencyTree();
					logger.info("EmergencyTree----------reloading");
					instance.reloadEmergencyDept(deptDao);
				}
			}
		}
	
		return instance;
	}

	@Override
	protected TreeNode transform(Object info) {
		// 将Object对象转换为树的结点
		UserDept dept = (UserDept) info;
		TreeNode node = new TreeNode();
		node.setNodeId(dept.getUdId());
		if (dept.getTbUserDept() != null) {
			node.setParentId(dept.getTbUserDept().getUdId());
		}
		node.setBindData(dept);
		return node;
	}

	public void reloadEmergencyDept(UserDeptDAO deptDao) {
		List nodes;
		try {
			TbDeptType deptType = new TbDeptType();
			deptType.setDtId("0002");
			deptType.setDtName("应急部门");
			// nodes = deptDao.findByDeptType(new TbDeptType("0001", "药监内部"));
			nodes = deptDao.findByDeptType(deptType);
			super.reload(nodes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
